﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;


namespace Dominio.Entidades
{
    public partial class Consultas
    {
        public DataSet EjecutarConsulta(string ConsultaAEjecutar)
        {
            Dictionary<string, string> Diccionario = new Dictionary<string, string>();

            SqlConnection objConn = OpenDB();

            SqlDataAdapter daAuthors = new SqlDataAdapter(ConsultaAEjecutar, objConn);

            DataSet datos = new DataSet("Agencia");

            daAuthors.MissingSchemaAction = MissingSchemaAction.AddWithKey;
            daAuthors.Fill(datos, "Agencia");

            CloseBD(objConn);

            return datos;
        }

        public SqlConnection OpenDB() 
        {
            string sConnectionString;
            sConnectionString = "Data Source=localhost;Initial Catalog=Agencia;Integrated Security=SSPI;";
            SqlConnection objConn = new SqlConnection(sConnectionString);
            objConn.Open();
            return objConn;
        }

        public void CloseBD(SqlConnection BD)
        {
            BD.Close();
        }

        public bool Exists()
        {
            bool existe = false;
            List<Consultas> ListaConsultas = Contexto.GetContexto().Consultas.ToList();
            if (ListaConsultas.Exists(o => o.Descripcion.Equals(this.Descripcion)))
            {
                existe = true;
            }
            return existe;
        }

        public void GuardarConsulta()
        {
            AgenciaEntities contexto = Contexto.GetContexto();
            contexto.Consultas.AddObject(this);
        }

        public override string ToString()
        {
            return this._Descripcion;
        }
    }
}
